
global user "`c(username)'"
global dirdata "C:\Users\\$user\Dropbox\CCT BJP\data"
global dir1  "$dirdata\Final data"
global dir2  "$dirdata\Results\graphs"
global dir3  "$dirdata\Results\"
cd "$dir3"

use "$dir1\pooled_hh.dta", clear
keep if tau>-5  
g age=age_m if hhmale==0
replace age=age_p if hhmale==1
g spanish=spanish_m if hhmale==0
replace spanish=spanish_p if hhmale==1

g Post=tau>=0
egen w_hoursw_head=rowtotal(w_hoursw_p w_hoursw_m)
g work_head=(work_m+work_p>0)


local nwork_lab "# of working adults"
local w_hoursw_t_lab "Hours/week"
local work_head_lab "Worked last week"
local w_hoursw_head_lab "Hours/week"
local work_m_lab "Worked last week"
local work_p_lab "Worked last week"
local w_hoursw_m_lab "Hours/week "
local w_hoursw_p_lab "Hours/week "
local parm Post
local replace replace

global covs0 "i.year "
global covs4 "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"
 
local spec0  Post i.min_schooling  $covs0  
local spec4  Post i.min_schooling  $covs4  

local sample1 "(min_schooling>=1 & min_schooling<9)"

local top1 "All"



local replace replace
foreach x in work_m w_hoursw_m w_ylabor_m {
foreach y in 0 4 {
sum `x' [w=factor]  if tau==-1 & `sample1'
local mean_dv=r(mean)
areg `x'  `spec`y'' [w=factor] if `sample1',a(departamento) cluster(municipio)
estimates store r`x'`y'
outreg2 using "$dir3\Table3", excel `replace' ctitle(``x'_lab') keep(`parm') less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}
}


global covs0 "i.year"
global covs4 "age_p spanish_p schooling_p age  spanish  schooling urban np nchild5  hhmale age_sac c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"
 
local spec0  Post i.min_schooling  $covs0  
local spec4  Post i.min_schooling  $covs4  

local sample1 "(min_schooling>=1 & min_schooling<9)"


local replace append
foreach x in work_p w_hoursw_p w_ylabor_p {
foreach y in 0 4  {
sum `x' [w=factor]  if tau==-1 & `sample1'
local mean_dv=r(mean)
areg `x'  `spec`y'' [w=factor] if `sample1',a(departamento) cluster(municipio)
outreg2 using "$dir3\Table3", excel `replace' ctitle(``x'_lab') keep(`parm') less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
estimates store r`x'1

}
}

***** Testing for differences between regressions for females and males.
global covs1_m "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year   i.year"
global covs1_p "age_p spanish_p schooling_p age  spanish  schooling urban np nchild5  hhmale age_sac c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"

local spec0_m  Post i.min_schooling  i.year
local spec0_p  Post i.min_schooling  i.year  

local spec1_m  Post i.min_schooling  $covs1_m
local spec1_p  Post i.min_schooling  $covs1_p  
local sample1 "(min_schooling>=1 & min_schooling<9)"
global vars work w_hoursw w_ylabor
matrix T=J(6,2,.)
foreach y in 0 1 {
foreach g in m p{
foreach x in work w_hoursw w_ylabor {
*sum `x'_`g'   if tau==-1 & `sample1'
*local mean_dv=r(mean)
qui areg `x'_`g'  `spec`y'_`g'' [w=factor] if `sample1',a(departamento) cluster(municipio)
reg `x'_`g'  `spec`y'_`g'' i.departamento [w=factor]  if `sample1' & e(sample)==1
estimates store r`x'_`g'
}
}
local c1=`y'+1
forvalues i=1/3{
local r2=`i'*2
local r1=`r2'-1
local x: word `i' of $vars
suest r`x'_m r`x'_p, cluster(municipio)
test ([r`x'_m_mean]_b[Post ] = [r`x'_p_mean]_b[Post ])
matrix T[`r1',`c1']=`r(chi2)'
matrix T[`r2',`c1']=`r(p)'
}
}
mat list T, format(%4.3f)
matrix rowname T=work-Chi2 work-p w_hoursw-Chi2 w_hoursw-p w_ylabor-Chi2 w_ylabor-p
matrix colname T= 1 2
putexcel set "$dir3\PTable3_chow", replace
putexcel A1=matrix(T),  nformat(number_d3) names 





